package cn.kgc.crowdfundingadminspringboot2.service;

import cn.kgc.crowdfundingadminspringboot2.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot2.mapper.RoleMapper;
import cn.kgc.crowdfundingadminspringboot2.pojo.Role;
import cn.kgc.crowdfundingadminspringboot2.pojo.RoleExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceimpl implements RoleService{
    @Resource
    RoleMapper roleMapper;

    @Override
    public PageInfo<Role> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        RoleExample roleExample=new RoleExample();
        RoleExample.Criteria criteria=roleExample.createCriteria();
        criteria.andNameLike("%"+keyword+"%");
        List<Role> roles = roleMapper.selectByExample(roleExample);
        PageInfo info=new PageInfo(roles);
        return info;
    }

    @Override
    public List<Role> getRoleListByIdList(List<Integer> roleIdList) {
        RoleExample example=new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria().andIdIn(roleIdList);

        return roleMapper.selectByExample(example);
    }

    @Override
    public void batchRemove(List<Integer> roleIdList) {
        RoleExample example=new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria().andIdIn(roleIdList);
        roleMapper.deleteByExample(example);
    }

    @Override
    public void saveRole(String roleName) {
        roleMapper.insert(new Role(null,roleName));
    }

    @Override
    public void updateRole(Role role) {

        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public List<Role> getAssignedRoleList(Integer adminId) {
        return roleMapper.getAssignedRoleList(adminId);
    }

    @Override
    public List<Role> getUnAssignedRoleList(Integer adminId) {
        return roleMapper.getUnAssignedRoleList(adminId);
    }

    @Override
    public void deletAdminRelationRole(Integer adminId) {
        roleMapper.deletAdminRelationRole(adminId);
    }

    @Override
    public void insertAdminRelationRole(Integer adminId, List<Integer> roleIdList) {
        roleMapper.insertAdminRelationRole(adminId,roleIdList);
    }
}
